package com.ylbh.modules.activity.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.activity.dto.ActivityGoodsDTO;
import com.ylbh.common.pojo.activity.dto.BatchPurchaseGoodsDTO;
import com.ylbh.common.pojo.activity.dto.GoodsListDTO;
import com.ylbh.common.pojo.activity.vo.BatchPurchaseGoodsVO;
import com.ylbh.common.pojo.goods.dto.AccessoryDTO;
import com.ylbh.common.pojo.goods.dto.GoodsClassDTO;
import com.ylbh.common.pojo.goods.dto.SubGoodsClassDTO;
import com.ylbh.common.pojo.goods.vo.ActivitySelectGoodsListVO;
import com.ylbh.common.pojo.goods.vo.GoodsPictureVO;
import com.ylbh.common.utils.PageDTO;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.goods.mapper.GoodsClassMapper;
import com.ylbh.modules.goods.mapper.ShoppingGoodsMapper;
import com.ylbh.modules.goods.service.GoodsSpecService;
import com.ylbh.modules.activity.mapper.ActivityGoodsMapper;
import com.ylbh.modules.activity.service.ActivityGoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("activityGoodsService")
public class ActivityGoodsServiceImpl extends ServiceImpl<ActivityGoodsMapper, GoodsClassDTO> implements ActivityGoodsService {

    @Autowired
    private ActivityGoodsMapper activityGoodsMapper;
    @Autowired
    private GoodsSpecService goodsSpecService;

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Autowired
    private ShoppingGoodsMapper shoppingGoodsMapper;

    /**
     * 获取分类列表
     * @return
     */
    @Override
    public List<GoodsClassDTO> queryClassList() {
        return activityGoodsMapper.selectClassList();
    }

    /**
     * 获取商品列表
     * @param goodsListDTO
     * @return
     */
    @Override
    public PageUtils queryGoodsList(GoodsListDTO goodsListDTO) {
        int limit = goodsListDTO.getLimit();
        int page = goodsListDTO.getPage();
        page = (page) * limit;
        goodsListDTO.setPage(page);
        String key = goodsListDTO.getKey();
        if(StringUtils.isNotBlank(key)){

            StringBuilder temp = new StringBuilder().append("%");
            for (int i = 0;i<key.length();i++){
                String substring = key.substring(i, i + 1);
                if(StringUtils.isNotBlank(substring)){
                    temp.append(substring).append("%");
                }
            }
            goodsListDTO.setKey(temp.toString());
        }
        List<ActivitySelectGoodsListVO> goodsLists = activityGoodsMapper.selectGoodsList(goodsListDTO);

        for (ActivitySelectGoodsListVO goods:goodsLists) {
            String sku = goods.getSku();
            goods.setSku(goodsSpecService.sortOutSku(sku));
        }

        return new PageUtils(goodsLists,
                activityGoodsMapper.getGoodsListCount(goodsListDTO),
                limit,
                page);
    }

    @Override
    public ActivityGoodsDTO getActivityGoodsById(Integer id) {
        return activityGoodsMapper.getActivityGoodsById(id);
    }

    /**
     * 添加批量采购商品
     * @param batchPurchaseGoodsDTO
     */
    @Override
    public void addBatchPurchaseGoods(BatchPurchaseGoodsDTO batchPurchaseGoodsDTO) {
        activityGoodsMapper.saveBatchPurchaseGoods(batchPurchaseGoodsDTO);
    }

    /**
     * 获取批量采购商品列表
     * @param goodsListDTO
     * @return
     */
    @Override
    public PageUtils queryBatchPurchaseGoodsList(GoodsListDTO goodsListDTO) {
        int limit = goodsListDTO.getLimit();
        int page = goodsListDTO.getPage();
        page = (page) * limit;
        goodsListDTO.setPage(page);

        return new PageUtils(activityGoodsMapper.selectBatchPurchaseGoodsList(goodsListDTO),
                activityGoodsMapper.selectBatchPurchaseGoodsCount(goodsListDTO),
                limit,
                page);
    }

    /**
     * 移除批量采购商品
     * @param ids
     */
    @Override
    public void removeBatchPurchaseGoods(Integer[] ids) {
        activityGoodsMapper.deleteBatchPurchaseGoods(ids);
        // 删除采购中间表信息
        activityGoodsMapper.deleteGoodsPurchase(ids);
    }

    /**
     * 采购一级分类列表
     * @param dto
     * @return
     */
    @Override
    public PageUtils queryPurchaseLargeClass(PageDTO dto) {
        Page<Object> page = new Page<>(dto.getPage(), dto.getLimit());
        List<SubGoodsClassDTO> list = goodsClassMapper.selectPurchaseLargeClass(dto.getKeywords(), page);
        return new PageUtils(new Page<SubGoodsClassDTO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 新增采购一级分类
     * @param subGoodsClassDTO
     */
    @Override
    public void addPurchaseLargeClass(SubGoodsClassDTO subGoodsClassDTO) {
        subGoodsClassDTO.setAddTime(new Date());
        subGoodsClassDTO.setRecommend(2);
        subGoodsClassDTO.setDisplay(0);
        AccessoryDTO accessoryDTO = new AccessoryDTO();
        accessoryDTO.setAddTime(new Date());
        shoppingGoodsMapper.insertGoodPhoto(accessoryDTO);
        subGoodsClassDTO.setIconAccId(accessoryDTO.getId().intValue());
        goodsClassMapper.insert(subGoodsClassDTO);
    }

    /**
     * 修改采购一级分类
     * @param subGoodsClassDTO
     */
    @Override
    public void modifyPurchaseLargeClass(SubGoodsClassDTO subGoodsClassDTO) {
        goodsClassMapper.updateById(subGoodsClassDTO);
    }

    /**
     * 删除采购一级分类
     * @param id
     */
    @Override
    public void removePurchaseLargeClass(List<Integer> id) {
        goodsClassMapper.updatePurchaseLargeClassStatus(id);
    }

    /**
     * 采购分类显示与隐藏
     * @param id
     * @param display
     */
    @Override
    public void modifyDisplayById(Integer id, Integer display) {
        SubGoodsClassDTO sgc = new SubGoodsClassDTO();
        sgc.setId(id);
        sgc.setDisplay(display);
        goodsClassMapper.updateById(sgc);
    }

    /**
     * 查看分类详情
     * @param id
     */
    @Override
    public SubGoodsClassDTO getPurchaseLargeClassById(Integer id) {
        SubGoodsClassDTO subGoodsClassDTO = goodsClassMapper.selectById(id);
        return subGoodsClassDTO;
    }

    /**
     * 采购二级分类列表
     * @param dto
     * @return
     */
    @Override
    public PageUtils queryPurchaseSecondLevelClass(PageDTO dto) {
        Page<Object> page = new Page<>(dto.getPage(), dto.getLimit());
        List<SubGoodsClassDTO> list = goodsClassMapper.queryPurchaseSecondLevelClass(dto.getKeywords(), page);
        return new PageUtils(new Page<SubGoodsClassDTO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 获取采购分类
     * @return
     */
    @Override
    public List<GoodsClassDTO> queryPurchaseClassList() {
        return activityGoodsMapper.queryPurchaseClassList();
    }

    /**
     * 根据批采分类查询出下级未添加过的商品列表
     * @param dto
     * @return
     */
    @Override
    public PageUtils queryLevelClassGoodsList(GoodsListDTO dto) {
        Page<Integer> page = new Page<>(dto.getPage(), dto.getLimit());
        List<ActivitySelectGoodsListVO> list = activityGoodsMapper.selectLevelClassGoodsList(dto, page);
        return new PageUtils(new Page<ActivitySelectGoodsListVO>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 添加商品到采购分类
     * @param classId
     * @param goodsList
     */
    @Override
    public void addPurchaseGoods(Integer classId, List<String> goodsList) {
        if (null != classId && goodsList.size() > 0) {
            for (String goodsId:goodsList) {
                activityGoodsMapper.insertPurchaseGoods(classId, goodsId);
            }
        }
    }

    /**
     * 根据采购分类查询商品列表
     * @param dto
     * @return
     */
    @Override
    public PageUtils queryPurchaseGoods(GoodsListDTO dto) {
        Page<Integer> page = new Page<>(dto.getPage(), dto.getLimit());
        List<BatchPurchaseGoodsVO> list = activityGoodsMapper.selectPurchaseGoods(dto, page);
        return new PageUtils(new Page<BatchPurchaseGoodsVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 删除批采分类下的商品
     * @param classId
     * @param goodsId
     */
    @Override
    public void removePurchaseClassGoodsByClassAndGoodsId(String classId, String goodsId) {
        activityGoodsMapper.deletePurchaseClassGoodsByClassAndGoodsId(classId, goodsId);
    }
}
